<?php

class Kd_Html_Form_Datetime extends Kd_Html_Form_Element {

    private $_format = 'Y-m-d';

    public function __construct() {
        parent::__construct();
        $this->templateName = 'form/datetime';
        $this->addClass('input', 'textbox', 'datetime');
    }

    public function getFormat(){
        return $this->_format;
    }

    public function setFormat($value){
        $this->_format = $value;
        return $this;
    }

    public function setCurrentDate(){
        $this->setValue(date('Y-m-d'));
        return $this;
    }

    protected function renderReadonly() {
        return '<span class="readonly">' . $this->getValue() . '</span>';
    }

    protected function renderEditable() {
        if (!$this->getEnabled()) {
            $this->addClass('disabled');
        }
        $this->setAttribute('type', 'text')
                ->setAttribute('id', $this->getId())
                ->setAttribute('name', $this->getName());
        if($this->getValue()){
            $this->setAttribute('value', date($this->getFormat(), strtotime($this->getValue())));
        }
        if (!$this->getEnabled()) {
            $this->setAttribute('disabled', 'disabled');
        }
        return '<input ' . $this->getAttributeHtml() . '/>';
    }

}